#!/user/bin/env node

/*
 * @Author: tenence.wu 
 * @Date: 2020-09-06 10:43:26 
 * @Last Modified by:   tenence.wu 
 * @Last Modified time: 2020-09-06 10:43:26 
 */


const inquirer = require('inquirer')
const path = require('path')
const ejs = require('ejs')
const fs = require('fs');

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'Project name?'
    },
    {
        type: 'list',
        name: 'language',
        message: 'Select language',
        choices: ['JavaScript', 'TypeScript'],
        filter: function (val) {
            return val.toLowerCase();
        },
    },
    {
        type: 'checkbox',
        message: 'Select color ',
        name: 'color',
        choices: [{
                name: 'red',
            },
            {
                name: 'yellow',
            },
            {
                name: 'blue',
            },
        ],
        validate: function (answer) {
            if (answer.length !== 1) {
                return 'You must choose one color.';
            }

            return true;
        },
    },
]).then(answer => {
    const tempDir = path.join(__dirname, 'templates')
    const destDir = process.cwd()

    fs.readdir(tempDir, (err, files) => {
        if (err) throw err
        files.forEach(file => {
            ejs.renderFile(path.join(tempDir, file), answer, (err, result) => {
                if (err) throw err
                fs.writeFileSync(path.join(destDir, file), result)
            })
        });
    })
})